//
//  TotalViewController.m
//  PrivateSteward
//
//  Created by lanou3g on 16/3/23.
//  Copyright © 2016年 闫贺晨. All rights reserved.
//

#import "TotalViewController.h"
#import "ZLSwitchCityDropDownMenu.h"
#import "UIView+ZLFrame.h"
#import "ZLSwitchCityDropDownCityView.h"
#import "ZLSwitchCitySearchView.h"
#import "Masonry.h"
#import "ZLCommonConst.h"
#import "MJExtension.h"
#import "ZLRegion.h"
#import "ZLCity.h"
#import "Weather.h"
#import "XMFDropBoxView.h"
#import "AddViewController.h"
@interface TotalViewController () <UITableViewDelegate,XMFDropBoxViewDataSource>
@property(nonatomic,weak) ZLSwitchCityDropDownMenu *meun;
@property (nonatomic, copy) NSString *selectedCityName;
@property (nonatomic, copy) NSString *selectedRegionName;
@property (nonatomic, strong) AddViewController *addVC;
@property(nonatomic,strong) NSMutableArray *myArray;
- (IBAction)chooseClick:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *chooseButter;
@property (weak, nonatomic) IBOutlet UILabel *tmpLabel;
@property (weak, nonatomic) IBOutlet UIButton *weatherButter;

@property (weak, nonatomic) IBOutlet UILabel *txtLabel;

@property (weak, nonatomic) IBOutlet UILabel *pm25Label;

@property (weak, nonatomic) IBOutlet UIImageView *txtImage;

@property (weak, nonatomic) IBOutlet UIView *WeatherView;

@property (weak, nonatomic) IBOutlet UIView *sthTableView;
- (IBAction)memoButton:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UIButton *memoButton;
//点击事件紧急程度
@property (nonatomic , copy)NSString *amountString;

@end

@implementation TotalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectedCityName = @"";
   
    [NS_NOTIFICATION_CENTER addObserver:self selector:@selector(cityDidChanged:) name:ZLCityDidChangeNotification object:nil];

    [NS_NOTIFICATION_CENTER addObserver:self selector:@selector(regionDidChanged:) name:ZLRegionDidChangeNotification object:nil];
    self.tmpLabel.hidden = YES;
    self.txtLabel.hidden = YES;
    self.pm25Label.hidden = YES;
    
    
    
    //设置tableView
   // [self sthTableViewdata];
    
    //请求天气数据
    [self Weatherdata];
    
    
}

- (void)Weatherdata{
   
    self.myArray = [[NSMutableArray alloc] init];
    if ( [self.selectedCityName isEqualToString:@""] == 0) {
  
    NSString *httpurl = @"http://apis.baidu.com/heweather/weather/free";
    NSString *httpArg = self.selectedCityName;
    NSString *method = @"post";
    NSString *myAPIkey = @"72e63a4eb91ff23fb618a4d946eff195";
    NSMutableDictionary *parameter = [[NSMutableDictionary alloc] init];
    [parameter setObject:httpArg forKey:@"city"];
    APISCallBack *callBack = [APISCallBack alloc];
    callBack.onSuccess = ^(long status, NSString* responseString)
    {
     
        
        NSData *tempData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
        
        NSMutableDictionary  *temoDictionary = [NSJSONSerialization JSONObjectWithData:tempData options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",temoDictionary);
        NSMutableArray *tempArray = [[NSMutableArray  alloc] init];
        tempArray  = [temoDictionary[@"HeWeather data service 3.0"] valueForKey:@"hourly_forecast"];
        NSMutableArray *tempArray1 = [temoDictionary[@"HeWeather data service 3.0"] valueForKey:@"suggestion"];
        NSMutableArray *tempArray2 = [temoDictionary[@"HeWeather data service 3.0"] valueForKey:@"daily_forecast"];
        NSMutableDictionary *tempDitnary = [temoDictionary[@"HeWeather data service 3.0"] valueForKey:@"astro"];
        NSLog(@"%@",tempDitnary);
        for (int i = 0; i < [tempArray1 count]; i++) {
            Weather *tempWeather = [[Weather alloc] init];
            //风力数据
            [tempWeather  setValuesForKeysWithDictionary:tempArray[0][i]];
            //穿衣信息
            [tempWeather setValuesForKeysWithDictionary:tempArray1[0][@"drsg"]];
            //天气信息日出日落时间
            [tempWeather setValuesForKeysWithDictionary:[tempArray2[0][i]valueForKey:@"astro"]];
            [tempWeather setValuesForKeysWithDictionary:[tempArray2[0][i]valueForKey:@"tmp"]];
            [tempWeather setValuesForKeysWithDictionary:[tempArray2[0][i]valueForKey:@"wind"]];
            [tempWeather setValuesForKeysWithDictionary:[tempArray2[0][i]valueForKey:@"cond"]];
            tempWeather.date = tempArray2[0][i][@"date"];
            
            if (i == 0) {
            [tempWeather setValuesForKeysWithDictionary:[[temoDictionary[@"HeWeather data service 3.0"] valueForKey:@"aqi"] valueForKey:@"city"][0]];
           
            //现在的天气风向
            [tempWeather  setValuesForKeysWithDictionary: [[temoDictionary[@"HeWeather data service 3.0"]valueForKey:@"now"][0]valueForKey:@"wind"]];
            //实时天气信息
            tempWeather.txt = [[[temoDictionary[@"HeWeather data service 3.0"]valueForKey:@"now"][0]valueForKey:@"cond"] valueForKey:@"txt"];
               
          
                self.tmpLabel.hidden = NO;
                self.txtLabel.hidden = NO;
                self.pm25Label.hidden = NO;
       
                self.tmpLabel.text = [NSString stringWithFormat:@"%@°~%@°", tempWeather.min,tempWeather.max];
                self.pm25Label.text = [NSString stringWithFormat:@"PM:%@",tempWeather.pm25];
                self.txtLabel.text = [NSString stringWithFormat:@"%@  %@",tempWeather.txt,tempWeather.qlty];
                self.txtImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://files.heweather.com/cond_icon/%@.png",tempWeather.code_d]]]];
                NSLog(@"%d",[tempWeather.code_d intValue]);
                if (  100 == [tempWeather.code_d intValue]) {
                    
                    
                    self.WeatherView.backgroundColor = [UIColor colorWithRed:0.5373 green:0.749 blue:0.8549 alpha:1.0];
                    
                }else if (101 >= [tempWeather.code_d intValue]&& 104 <= [tempWeather.code_d intValue]){
                
                
                    self.WeatherView.backgroundColor = [UIColor colorWithRed:0.4918 green:0.5016 blue:0.5283 alpha:1.0];
                    self.weatherButter.titleLabel.textColor= [UIColor colorWithRed:0.4271 green:0.9495 blue:1.0 alpha:1.0];
     
                }else if (205 >= [tempWeather.code_d intValue]&& 305 <= [tempWeather.code_d intValue]){
                
                
                    self.WeatherView.backgroundColor = [UIColor colorWithRed:0.1961 green:0.2196 blue:0.2314 alpha:1.0];
                    self.weatherButter.tintColor = [UIColor colorWithRed:0.5021 green:0.9333 blue:1.0 alpha:1.0];
              
                }else if (506 >= [tempWeather.code_d intValue]&& 508 <= [tempWeather.code_d intValue]){
                
                
                    self.WeatherView.backgroundColor = [UIColor colorWithRed:0.8314 green:0.6157 blue:0.2039 alpha:1.0];
                
                }else if (400 >= [tempWeather.code_d intValue]&& 407 <= [tempWeather.code_d intValue]){
                
                
                    self.WeatherView.backgroundColor = [UIColor colorWithRed:0.4157 green:0.5882 blue:0.7294 alpha:1.0];
                
                }else if(501 >= [tempWeather.code_d intValue] && 502 <= [tempWeather.code_d intValue]){
                
                
                    self.WeatherView.backgroundColor = [UIColor colorWithRed:0.6718 green:0.6981 blue:0.7191 alpha:1.0];
                
                
                }
                
                
            }

            //将数据放入数据源
            [self.myArray addObject:tempWeather];
                    }

    };
    
    [ApiStoreSDK executeWithURL:httpurl method:method apikey:myAPIkey parameter:parameter callBack:callBack];
        
    }
   
    
  
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   //获取ViewContorller
    AddViewController *addVC = segue.destinationViewController;
    //传值过去
    addVC.amount = self.amountString;
    
    NSLog(@"%@",addVC.amount);
    
}

//点击选择城市方法
- (IBAction)chooseClick:(UIButton *)sender {
    
    //初始化
    ZLSwitchCityDropDownMenu *menu = [ZLSwitchCityDropDownMenu menu];
    //初始化弹出框
    ZLSwitchCityDropDownCityView *cityView = [ZLSwitchCityDropDownCityView dropDownCityView];
    //设置宽高
    cityView.zl_rectWidth = 300;
    cityView.zl_rectHeight = 300;
    
    ZLCity *city = [[[ZLCity objectArrayWithFilename:ZLSwitchCitySourceName(@"plists/cities.plist")] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name = %@", self.selectedCityName]] firstObject];
    cityView.regions = city.regions;
    menu.content = cityView;
    [menu showFrom:sender didShow:nil];
    [cityView setupDidChangeCityHandler:^(UIButton *button) {
        [menu dismiss];
        [[ZLSwitchCitySearchView searchView] show];
    }];
    self.meun = menu;

    
}
- (void)regionDidChanged:(NSNotification *)notification
{
    ZLRegion *region = notification.userInfo[ZLSelectRegion];
    NSString *subregionName = notification.userInfo[ZLSelectSubregionName];
    
    if (subregionName == nil || [subregionName isEqualToString:@"全部"]) {
        
        self.selectedRegionName = region.name;
        
    } else {
        self.selectedRegionName = subregionName;
       
       
    }
    if ([self.selectedRegionName isEqualToString:@"全部"]) {
        self.selectedRegionName = nil;
    }
    [self.meun dismiss];
    
    
}

- (void)cityDidChanged:(NSNotification *)notification
{
    self.selectedCityName = notification.userInfo[ZLSelectCityName];
    //设置城市名称
    [self.chooseButter setTitle:self.selectedCityName forState: UIControlStateNormal];
    [self Weatherdata];
}

//点击弹窗wdawdawd
- (IBAction)memoButton:(UIButton *)sender {
    
    XMFDropBoxView *inputBox = [XMFDropBoxView dropBoxWithLocationView:sender dataSource:self];
    [inputBox selectItemWithBlock:^(NSUInteger index) {
        
        if (index == 0) {
         
           self.amountString = @"普通";
            
            
            
        }else if (index == 1){
        
            self.amountString = @"紧急";
        
        
        }else{
        
        
         self.amountString = @"重大";
        
        }
        
        AddViewController *addVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Add"];
        addVC.amount =self.amountString;
         [self.navigationController pushViewController:addVC animated:YES];
        
        
        
    }];
    [inputBox displayDropBox];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [XMFDropBoxView removeAllDropBox];
}



- (CGFloat)dropBoxView:(XMFDropBoxView *)dropBoxView heightForItemAtIndex:(NSUInteger)index {
    return 44.f;
}

- (UIView *)dropBoxView:(XMFDropBoxView *)dropBoxView itemAtIndex:(NSUInteger)index {
    UILabel *titleLB = [[UILabel alloc] init];
    titleLB.textAlignment = NSTextAlignmentCenter;
    titleLB.font = [UIFont systemFontOfSize:20];
    UIView *view = [[UIView  alloc] init];
    view.backgroundColor = [UIColor   whiteColor];
    UIImageView *LimageImage = [[UIImageView alloc] init];
    LimageImage.frame = CGRectMake(0, 0, 44, 44);
    if (index == 0) {
        
        titleLB.text = @"普通";
        LimageImage.image = [UIImage imageNamed:@"lan.png"];
        
    }else if (index == 1){
    
        titleLB.text = @"紧急";
        LimageImage.image = [UIImage imageNamed:@"huang.png"];
    
    
    }else{
    
    
        titleLB.text = @"重大";
        LimageImage.image = [UIImage imageNamed:@"hong.png"];
    
    }
    
    titleLB.frame = CGRectMake(44, 0, 100, 44);
    [view addSubview:titleLB];
    [view addSubview:LimageImage];
    return view;
}

- (CGFloat)widthInDropBoxView:(XMFDropBoxView *)dropBoxView {
    return 144.f;
}
- (NSUInteger)numberOfItemInDropBoxView:(XMFDropBoxView *)dropBoxView {
    return 3;
}




@end
